﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Text;
using System.Security.Cryptography;

using ISS.CaseStudy.BLL;


namespace CaseStudy.AdminPortal
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            txtUserName.Focus();
            if (!IsPostBack)
            {
                HttpCookie cookie = Request.Cookies["cnuser"];
                if (cookie != null && cookie.Values["UserId"] != null)
                {
                    pnlLogin.Visible = false;
                    pnlWelcome.Visible = true;
                }
                else
                {
                    pnlLogin.Visible = true;
                    pnlWelcome.Visible = false;
                }
            }
        }

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            string userName = HttpUtility.HtmlEncode(txtUserName.Text);
            string password = Encypt(HttpUtility.HtmlEncode(txtPassword.Text));

            txtUserName.Text = "";
            txtPassword.Text = "";

            DataSet dataSet = UserInformationBLL.GetAll();
            foreach (DataRow row in dataSet.Tables[0].Rows)
            {
                if (row["Name"].ToString().Equals(userName))
                {
                    if (row["Password"].ToString().Equals(password))
                    {
                        pnlLogin.Visible = false;
                        pnlWelcome.Visible = true;

                        HttpCookie cookie = new HttpCookie("cnuser");
                        cookie.Values["UserId"] = row["Id"].ToString();
                        cookie.Expires = DateTime.Now.AddHours(2);

                        if (Response.Cookies["cnuser"] != null)
                        {
                            Response.Cookies.Remove("cnuser");
                        }
                        Response.Cookies.Add(cookie);

                        loginInfo.Refresh();
                    }
                    else
                    {
                        lblError.Text = "密码错误！";
                        lblError.Visible = true;
                    }

                    return;
                }
            }

            lblError.Text = "该用户还没有注册！";
            lblError.Visible = true;
            return;
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            txtUserName.Text = "";
            txtPassword.Text = "";
        }

        private string Encypt(string key)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] InBytes = Encoding.GetEncoding("GB2312").GetBytes(key);
            byte[] OutBytes = md5.ComputeHash(InBytes);
            string OutString = "";
            for (int i = 0; i < OutBytes.Length; i++)
            {
                OutString += OutBytes[i].ToString("x2");
            }
            return OutString;
        }
    }
}
